<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

use think\Db;

function echoJson($code, $msg,$result=[]) {
    header('Content-Type:application/json; charset=utf-8');

    $arr = array("code" => $code, "msg" => $msg, "result"=>$result);

    echo json_encode($arr, JSON_UNESCAPED_UNICODE);

    exit;
}

function tt_random_data($num,$table,$where=[])
{
    $pk = Db::name($table)->getPK();//获取主键
    $countcus = Db::name($table)->where($where)->field($pk)->select();//查询数据
    $con = '';
    $qu = '';
    foreach($countcus as $v=>$val){
        $con.= $val[$pk].'|';
    }
    $array = explode("|",$con);
    $countnum = count($array)-1;
    for($i = 0;$i <= $num;$i++){
        $sunum = mt_rand(0,$countnum);
        $qu.= $array[$sunum].',';
    }
    $list = Db::name($table)->where($pk,'in',$qu)->select();
    return $list;
}